package com.dys.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: 疾风
 * @ClassName: Servlet01
 * @Description: 请求转发的路径问题
 * @CreateDate: 2024/09/20
 * @Version: v1.0.0
 */
@WebServlet(urlPatterns = "/servlet01.do")
public class Servlet01 extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行Servlet01内的service方法......");
        /*
         * 请求转发的路径写法
         * 相对基准路径:相对于当前Servlet本身的位置,urlPattern决定了位置
         * 绝对基准路径:永远是以项目为基准路径(不允许跨服务,所以绝对路径只能是本服务内的资源)
         * */
        // 相对路径访问a1.html
        // 响应重定向写法
        // resp.sendRedirect("a/a2/a1.html");
        // 请求转发写法
        // RequestDispatcher requestDispatcher = req.getRequestDispatcher("a/a2/a1.html");
        // requestDispatcher.forward(req, resp);
        // 绝对路径写法
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/a/a2/a1.html");
        requestDispatcher.forward(req, resp);
    }
}
